package cn.com.generaldata.os.service.indicator_evaluation.prosperity_analysis;

import java.util.List;

import jsme_matlab.Calculate;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

/**
 * 滞后组景气指数评估指标计算类.
 */
public class LagProsperityIndexCalculateService extends IndicatorEvaluationCalculateService {

	private static Logger log = LoggerFactory.getLogger(LagProsperityIndexCalculateService.class);

	private int baseYear = 2002;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	/**
	 * 计算江苏省进出口总额增长率评估指标计算类.
	 * 
	 * @param indicator
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Calculate cal = null;
		Double val = null;
		double[][] beforehand = getProsperityIndicatorData(indicatorAccountVO,
				ProvinceIndicatorConstant.BEFOREHAND_INDICATOR_NAMES);
		double[][] consistency = getProsperityIndicatorData(indicatorAccountVO,
				ProvinceIndicatorConstant.CONSISTENCY_INDICATOR_NAMES);
		double[][] lag = getProsperityIndicatorData(indicatorAccountVO, ProvinceIndicatorConstant.LAG_INDICATOR_NAMES);
		try {
			cal = new Calculate();
			Object[] result = cal.CI(3, beforehand, consistency, lag);
			MWNumericArray r = (MWNumericArray) result[2];
			int index = indicatorAccountVO.getYear() - baseYear;
			if (index < 0) {
				return null;
			}
			// matlab 数组从1开始
			val = r.getDouble(index + 1);
		} catch (MWException e) {
			log.error("调用Matlab计算先行组景气指数指标错误", e);
		} finally {
			if (cal != null) {
				cal.dispose();
			}
		}
		return val;
	}

	/**
	 * 获取相关数据.
	 * 
	 * @param indicatorAccountVO
	 * @param indicatorNames
	 * @return
	 */
	private double[][] getProsperityIndicatorData(IndicatorAccountVO indicatorAccountVO, List<String> indicatorNames) {
		int dataYears = getEvaluationMaxYear(indicatorNames.get(0), indicatorAccountVO.getAreaId());
		int yearLength = dataYears - 2001;
		double[][] result = new double[yearLength][indicatorNames.size()];
		for (int i = 0, years = dataYears - baseYear + 1; i < years; i++) {
			for (int j = 0, length = indicatorNames.size(); j < length; j++) {
				String indicatorName = indicatorNames.get(j);
				Integer indicatorId = getIndicatorEvaluationId(indicatorName, indicatorAccountVO.getAreaId());
				Criteria cri = getIndicatorDataCri(indicatorAccountVO, baseYear + i, indicatorId);
				Double indicatorVal = getEvaluationIndicatorValue(cri);
				Validate.isTrue(indicatorVal != null);
				result[i][j] = indicatorVal;
			}
		}
		return result;
	}
}
